﻿using DHQGHN.DAL.Base;
using DHQGHN.DAL.Common;
using DHQGHN.DAL.Models.DHQG;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;



namespace DHQGHN.DAL.DAO.DHQG
{
    public class PhanQuyenVanBanDAO : SqlDAO
    {
        public PhanQuyenVanBanDAO(string connectionString, System.Type type = null) :
            base(connectionString, type) { ;}
        public const string PROCEDURE_INSERT = "spPhanQuyenVanBan_Insert";
        public const string PROCEDURE_GET_BY_VAN_BAN = "spPhanQuyenVanBan_GetbyVanBanID";
        public const string PROCEDURE_DELETE_BY_VAN_BAN = "spPhanQuyenVanBan_DeletebyVanBanID";

        public const string PARAM_TABLE = "@Table";
        public const string PARAM_DON_VI_BAN_HANH = "@DON_VI_BAN_HANH";
        public const string PARAM_DON_VI_TRUY_CAP = "@DON_VI_TRUY_CAP";
        public const string PARAM_VAN_BAN_ID = "@VAN_BAN_ID";
        public const string PARAM_NGUOI_TAO = "@NGUOI_TAO";
        public const string PARAM_NGUOI_SUA = "@NGUOI_SUA";

        public bool Save(List<PhanQuyenVanBan> data)
        {
            try
            {
                DataTable dataTable = GetDataTable(data);
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_INSERT);
                cmd.Parameters.Add(new SqlParameter(PARAM_TABLE, dataTable));
                int result = ExecuteNonQuery(cmd);

                return result > 0 ? true : false;
            }
            catch(Exception ex)
            {
                throw;
            }
        }
        public List<PhanQuyenVanBan> GetallbyVanban(int vanbanid) {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_GET_BY_VAN_BAN);
                cmd.Parameters.Add(CreateParameter(PARAM_VAN_BAN_ID, vanbanid));
                return GetList<PhanQuyenVanBan>(cmd);
            }
            catch
            {
                throw;
            }

        }
        public bool DeletebyVanban(int vanbanid)
        {
            try
            {
                SqlCommand cmd = this.GetSprocCommand(PROCEDURE_DELETE_BY_VAN_BAN);
                cmd.Parameters.Add(CreateParameter(PARAM_VAN_BAN_ID, vanbanid));
                int result = ExecuteNonQuery(cmd);

                return result > 0 ? true : false;
            }
            catch
            {
                throw;
            }

        }

        private DataTable GetDataTable(List<PhanQuyenVanBan> data)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("VAN_BAN_ID");
            dt.Columns.Add("DON_VI_BAN_HANH");
            dt.Columns.Add("DON_VI_TRUY_CAP");
            dt.Columns.Add("DELETED");
            dt.Columns.Add("NGUOI_TAO");
            dt.Columns.Add("NGAY_TAO");
            dt.Columns.Add("NGUOI_SUA");
            dt.Columns.Add("NGAY_SUA");
            foreach (var item in data)
            {
                dt.Rows.Add(item.VAN_BAN_ID, item.DON_VI_BAN_HANH, item.DON_VI_TRUY_CAP, item.DELETED, item.NGUOI_TAO,item.NGAY_TAO,item.NGUOI_SUA,item.NGAY_SUA);
            }
            return dt;
        }

    }
}
